<?php

namespace Admin\Controller;

use Common\Util\Version;

class VersionController extends BaseController
{
    public function index(){
        $Version = new Version;
        $where = [
            "status" => ["lt",2]
        ];
        $platform = I("platform",0);
        if( $platform )
        {
            $where['platform'] = $platform;
        }
        $model = M('app_version');
        $count = $model->where($where)->count();
        $Page = new \Think\Page($count,10);
        $list = $model->where($where)
        ->limit($Page->firstRow.','.$Page->listRows)
        ->order("id desc")
        ->select();
        foreach($list as $k => &$v)
        {
            $v['last_need_update_ver'] = $Version->int2Ver($v['last_need_update_ver']);
            $v['create_time'] = date("Y-m-d H:i:s",$v['create_time']);
            $v['publish_time'] = $v['publish_time'] > 0 ?date("Y-m-d H:i:s",$v['publish_time']):"";
        }
        $page = $Page->show();
       
        $this->assign("platform",$Version->platform_config);
        $this->assign("status",$Version->status_config);
        $this->assign('list',$list);
        $this->assign('page',$page);
        $this->assign('platform_set',$platform);
        $this->display();
    }

    public function add(){
        
        $Version = new Version;
        
        if(IS_POST){
            
            try{
                if( I("post.id") )
                {
                    $res = $Version->update(I('post.'));
                }else{
                    $res = $Version->add(I('post.'));
                }
            }catch(\Exception $e)
            {
                $this->error($e->getMessage());
            }
            $this->success("操作成功");
            return;
        }
        $platform_set = I('platform',1);

        $list = M('app_version')->where([
            "platform" => $platform_set,
            "status" => 1
        ])->field("id,app_ver,platform,app_ver_name,app_ver_num")->order("app_ver_num desc")->select();
        
        $id = I("get.id",0);

        

        if( $id )
        {
            $data = M('app_version')->where([
                "id" => $id
            ])->find();

            if(!$data)
            {
                $this->error("数据不存在");
            }

            if($data['status'])
            {
                $this->error("数据不允许更改");
            }

            $this->assign("data",$data);

            $platform_set = $data['platform'];
        }

        $this->assign("list",$list);
        $this->assign("platform_set",$platform_set);
        $this->assign("platform",$Version->platform_config);
        
        $this->display();
    }

    public function publish(){
         

        if(IS_POST){
            $Version = new Version;
            try{
                $res = $Version->publishVersion(I('post.'));
            }catch(\Exception $e)
            {
                $this->error($e->getMessage());
            }
            $this->success("发布成功");
            return;
        } 

        $this->error("非法操作");
    }

    public function del(){
        if(IS_POST){
            $Version = new Version;
            try{
                $res = $Version->removeVersion(I('post.'));
            }catch(\Exception $e)
            {
                $this->error($e->getMessage());
            }
            $this->success("删除成功");
            return;
        } 

        $this->error("非法操作");
    }

    public function uploadApp(){
        set_time_limit(0);
        ini_set("post_max_size", "70M");
        ini_set("upload_max_filesize", "70M");
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     73400320 ;// 设置附件上传大小
        $upload->exts      =     array('apk');// 设置附件上传类型
        $upload->rootPath  =     realpath(__ROOT__).'/Public/upload/'; // 设置附件上传根目录
        $upload->savePath  =     'app/android/'; // 设置附件上传（子）目录
        // 上传文件 
        $info   =   $upload->upload();
        if(!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        }else{// 上传成功
            $res = [];
            foreach($info as $file)
            {
                $res = $file['urlpath'];
            }

            $appObj = new \Common\Util\Apkparser();
            // var_dump($file);
            $targetFile = $file['tmp_name'];

            //apk所在的路径地址
            $appObj->open($targetFile);
            $result['appName'] = $appObj->getAppName();     // 应用名称
            $result['appPackage'] = $appObj->getPackage();    // 应用包名
            $result['appVersion'] = $appObj->getVersionName();  // 版本名称
            $result['appVersionCode'] = $appObj->getVersionCode();  // 版本代码

            // #上传到OSS
            // try{
            //     $oss = \Common\Util\Oss::upload("upload/app/android/{$file['savename']}",$targetFile);
            // }catch(\Exception $e)
            // {
            //     $this->error($e->getMessage());
            // }

            // if($oss)
            // {
            //     @unlink($targetFile);
            // }

            header('contentType:text/json');
            echo  json_encode([
                    'info' => '上传成功!',
                    'appInfo' => $result,
                    'url' => parseMediaUrl($res),
                    'status' => 1
                ]);
        }
    }
}